package com.ruoyi.warehouse.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.warehouse.domain.CpkcFnishedProductInventory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.warehouse.domain.FhjlShippingRecordForm;
import com.ruoyi.warehouse.service.IFhjlShippingRecordFormService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 成品发货Controller
 * 
 * @author ruoyi
 * @date 2024-07-04
 */
@RestController
@RequestMapping("/cpfh")
public class FhjlShippingRecordFormController extends BaseController
{
    @Autowired
    private IFhjlShippingRecordFormService fhjlShippingRecordFormService;

    /**
     * 查询成品发货列表
     */
    @RequiresPermissions("warehouse:cpfh:list")
    @GetMapping("/list")
    public TableDataInfo list(FhjlShippingRecordForm fhjlShippingRecordForm)
    {
        startPage();
        List<FhjlShippingRecordForm> list = fhjlShippingRecordFormService.selectFhjlShippingRecordFormList(fhjlShippingRecordForm);
        return getDataTable(list);
    }

    /**
     * 导出成品发货列表
     */
    @RequiresPermissions("warehouse:cpfh:export")
    @Log(title = "成品发货", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FhjlShippingRecordForm fhjlShippingRecordForm)
    {
        List<FhjlShippingRecordForm> list = fhjlShippingRecordFormService.selectFhjlShippingRecordFormList(fhjlShippingRecordForm);
        ExcelUtil<FhjlShippingRecordForm> util = new ExcelUtil<FhjlShippingRecordForm>(FhjlShippingRecordForm.class);
        util.exportExcel(response, list, "成品发货数据");
    }

    /**
     * 获取成品发货详细信息
     */
    @RequiresPermissions("warehouse:cpfh:query")
    @GetMapping(value = "/{fhCode}")
    public AjaxResult getInfo(@PathVariable("fhCode") String fhCode)
    {
        FhjlShippingRecordForm fhjlShippingRecordForm = fhjlShippingRecordFormService.selectCpkcFnishedProductInventoryCpCode(fhCode);
        if(fhjlShippingRecordForm==null){
            fhjlShippingRecordForm=new FhjlShippingRecordForm();
        }
        return success(fhjlShippingRecordForm);
    }

    /**
     * 获取成品发货详细信息
     */
    @GetMapping(value = "/tj/{cpCode}")
    public AjaxResult getCpxqTj(@PathVariable("cpCode") String cpCode)
    {
        FhjlShippingRecordForm fhjlShippingRecordForm = fhjlShippingRecordFormService.selectTj(cpCode);
//        判断返回的记录是否为空
        if(fhjlShippingRecordForm==null){
            fhjlShippingRecordForm=new FhjlShippingRecordForm();
            return success(fhjlShippingRecordForm);
        }
        for (CpkcFnishedProductInventory cpkc : fhjlShippingRecordForm.getCpkcList()) {
            cpkc.getFhslQuantityShipped().setFhNum(null);
        }
        return success(fhjlShippingRecordForm);
    }

    /**
     * 新增成品发货
     */
    @RequiresPermissions("warehouse:cpfh:add")
    @Log(title = "成品发货", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FhjlShippingRecordForm fhjlShippingRecordForm)
    {
        return toAjax(fhjlShippingRecordFormService.insertFhjlShippingRecordForm(fhjlShippingRecordForm));
    }

    /**
     * 修改成品发货
     */
    @RequiresPermissions("warehouse:cpfh:edit")
    @Log(title = "成品发货", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FhjlShippingRecordForm fhjlShippingRecordForm)
    {
        return toAjax(fhjlShippingRecordFormService.updateFhjlShippingRecordForm(fhjlShippingRecordForm));
    }

    /**
     * 删除成品发货
     */
    @RequiresPermissions("warehouse:cpfh:remove")
    @Log(title = "成品发货", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fhCodes}")
    public AjaxResult remove(@PathVariable String[] fhCodes)
    {
        return toAjax(fhjlShippingRecordFormService.deleteFhjlShippingRecordFormByFhCodes(fhCodes));
    }

    /**
     * 审核
     *
     * @param fhCode 审核
     * @return 结果
     */
    @RequiresPermissions("warehouse:cpfh:examine")
    @PostMapping("/examine/{fhCode}")
    public AjaxResult examine(@PathVariable String fhCode)
    {
        return toAjax(fhjlShippingRecordFormService.examine(fhCode));
    }

    /**
     * 撤销
     *
     * @param fhCode 撤销
     * @return 结果
     */
    @RequiresPermissions("warehouse:cpfh:cancel")
    @PostMapping("/cancel/{fhCode}")
    public AjaxResult cancel(@PathVariable String fhCode)
    {
        return toAjax(fhjlShippingRecordFormService.cancel(fhCode));
    }
}
